package org.glut.service;

import org.glut.util.GetApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class SerialPortMsg {

    @Autowired
    private  CFAirService cfAirService ;
    @Autowired
    private YHAirService yhAirService;
    @Autowired
    private HWJCService hwjcService;
    @Autowired
    private RFService rfService;
    @Autowired
    private GZService gzService;

    public static  List<String>  serialMegList = new ArrayList<>();

    public  void  getMessage(String s ){
        cfAirService = (CFAirService) GetApplicationContext.getBean("CFAirService");
        yhAirService = (YHAirService) GetApplicationContext.getBean("YHAirService");
        hwjcService = (HWJCService) GetApplicationContext.getBean("HWJCService");
        rfService = (RFService) GetApplicationContext.getBean("RFService");
        gzService = (GZService) GetApplicationContext.getBean("GZService");
//        s = s.substring(0,s.indexOf("E"));
        String[] messageArray = s.split("E");
        for (String str : messageArray) {
            String[] s1 = str.split(" ");
            String join = String.join(" ", s1);
            serialMegList.add(join);
             System.out.println(join);
            if ("CF2".equals(s1[0]) || "CF1".equals(s1[0])){
                if (cfAirService == null) {
                    System.out.println("cfAirService == null");
                }
                try {
                    cfAirService.getmsgs(s1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else if ("YH2".equals(s1[0]) || "YH1".equals(s1[0])){
                if (yhAirService == null) {
                    System.out.println("yhAirService == null");
                }
                try {
                    yhAirService.getmsgs(s1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else if ("HW2".equals(s1[0]) || "HW1".equals(s1[0])){
                if (hwjcService == null) {
                    System.out.println("hwjcService == null");
                }
                try {
                    hwjcService.getmsgs(s1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if ("RF2".equals(s1[0]) || "RF1".equals(s1[0])){
                if (rfService == null) {
                    System.out.println("rfjcService == null");
                }
                try {
                    rfService.getmsgs(s1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            else if ("GZ2".equals(s1[0]) || "GZ1".equals(s1[0])){
                if (gzService == null) {
                    System.out.println("rfjcService == null");
                }
                try {
                    gzService.getmsgs(s1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

    }
}
